package souralven.jcomicreader.ui;

import javax.swing.JButton;
import javax.swing.JToolBar;

import souralven.jcomicreader.controller.ComicController;
import souralven.jcomicreader.controller.listener.GoBackListener;
import souralven.jcomicreader.controller.listener.GoNextListener;
import souralven.jcomicreader.controller.listener.OpenFileListener;
import souralven.jcomicreader.controller.listener.WindowAdjustListener;
import souralven.jcomicreader.controller.listener.ZoomAdjustListener;
import souralven.jcomicreader.controller.listener.ZoomInListener;
import souralven.jcomicreader.controller.listener.ZoomOutListener;
import souralven.jcomicreader.util.ImageUtil;
import souralven.jcomicreader.util.MessageUtil;

public class ToolBar extends JToolBar {

	/**
	 * 
	 */
	private static final long serialVersionUID = 518643890324969980L;
	private JButton open = null;
	private JButton previous = null;
	private JButton next = null;
	private JButton zoomIn = null;
	private JButton zoomOut = null;
	private JButton zoomAdjust = null;
	private JButton windowAdjust = null;
	private ComicController controller = null;
	// TODO 1st button
	// TODO zoom combo
	// TODO GoTo combo

	public ToolBar(final ComicController controller) {
		this.controller = controller;
		initialize();
	}

	public void updateUI(final int imageNumber, final int max, final double zoomScale) {
		previous.setEnabled(imageNumber != 0);
		next.setEnabled(imageNumber < (max-1));
		zoomIn.setEnabled(ComicController.MAX_ZOOM_LEVEL > zoomScale);
		zoomOut.setEnabled(ComicController.MIN_ZOOM_LEVEL < zoomScale);
		zoomAdjust.setEnabled(true);
		windowAdjust.setEnabled(true);
	}

	private void initialize() {
		this.add(getOpen());
		this.addSeparator();
		this.add(getPrevious());
		this.add(getNext());
		this.addSeparator();
		this.add(getZoomIn());
		this.add(getZoomOut());
		this.add(getZoomAdjust());
		this.add(getWindowAdjust());
	}

	private JButton getOpen() {
		if (open == null) {
			open = new JButton(ImageUtil.getImage("Open", ImageUtil.GIF_EXTENSION));
			open.setToolTipText(MessageUtil.getMessage("open"));
			open.addActionListener(new OpenFileListener(controller));
		}
		return open;
	}

	private JButton getPrevious() {
		if (previous == null) {
			previous = new JButton(ImageUtil.getImage("Back", ImageUtil.GIF_EXTENSION));
			previous.setToolTipText(MessageUtil.getMessage("previous"));
			previous.addActionListener(new GoBackListener(controller));
			previous.setEnabled(false);
		}
		return previous;
	}

	private JButton getNext() {
		if (next == null) {
			next = new JButton(ImageUtil.getImage("Next", ImageUtil.GIF_EXTENSION));
			next.setToolTipText(MessageUtil.getMessage("next"));
			next.addActionListener(new GoNextListener(controller));
			next.setEnabled(false);
		}
		return next;
	}

	private JButton getZoomIn() {
		if (zoomIn == null) {
			zoomIn = new JButton(ImageUtil.getImage("ZoomIn", ImageUtil.GIF_EXTENSION));
			zoomIn.setToolTipText(MessageUtil.getMessage("zoom.in"));
			zoomIn.addActionListener(new ZoomInListener(controller));
			zoomIn.setEnabled(false);
		}
		return zoomIn;
	}

	private JButton getZoomOut() {
		if (zoomOut == null) {
			zoomOut = new JButton(ImageUtil.getImage("ZoomOut", ImageUtil.GIF_EXTENSION));
			zoomOut.setToolTipText(MessageUtil.getMessage("zoom.out"));
			zoomOut.addActionListener(new ZoomOutListener(controller));
			zoomOut.setEnabled(false);
		}
		return zoomOut;
	}

	public JButton getZoomAdjust() {
		if (zoomAdjust == null) {
			zoomAdjust = new JButton(ImageUtil.getImage("ZoomAdjust", ImageUtil.GIF_EXTENSION));
			zoomAdjust.setToolTipText(MessageUtil.getMessage("zoom.adjust"));
			zoomAdjust.addActionListener(new ZoomAdjustListener(controller));
			zoomAdjust.setEnabled(false);
		}
		return zoomAdjust;
	}

	public JButton getWindowAdjust() {
		if (windowAdjust == null) {
			windowAdjust = new JButton(ImageUtil.getImage("WindowAdjust", ImageUtil.GIF_EXTENSION));
			windowAdjust.setToolTipText(MessageUtil.getMessage("window.adjust"));
			windowAdjust.addActionListener(new WindowAdjustListener(controller));
			windowAdjust.setEnabled(false);
		}
		return windowAdjust;
	}
}
